package com.thebitstream.sandy.support;

import java.util.Collection;
import java.util.Set;

import org.red5.server.api.IConnection;
import org.red5.server.api.IScope;
import org.red5.server.api.Red5;

/**
 * @author Andy Shaules
 *
 */
public class PropertyService {
	public Object setProperty(Object key, Object value) {
		((ClientData) Red5.getConnectionLocal().getClient().getAttribute(Engine.DATA)).data.put(key, value);
		return value;
	}

	public Object getProperty(Object key) {
		return new Object[] { ((ClientData) Red5.getConnectionLocal().getClient().getAttribute(Engine.DATA)).data
				.get(key) };
	}

	public Object getAll() {
		return new Object[] { ((ClientData) Red5.getConnectionLocal().getClient().getAttribute(Engine.DATA)).data };
	}

	public Object getUserProperty(String user, Object key) {
		IScope scope = Red5.getConnectionLocal().getScope();
		Collection<Set<IConnection>> clients = scope.getConnections();

		for (Set<IConnection> connectionSet : clients) {
			for (final IConnection connection : connectionSet) {
				if (connection.getClient().getAttribute(Engine.ID).equals(user)) {
					return new Object[] { ((ClientData) connection.getClient().getAttribute(Engine.DATA)).data.get(key) };
				}

			}
		}
		return 0;
	}
}
